#coding=utf-8
#Boa:Dialog:DlgEvent

import wx

from product import CDataEvent
from ctrlAider import *
import DlgEventEditor

def create(parent):
    return DlgEvent(parent)

[wxID_DLGEVENT, wxID_DLGEVENTBTNADD, wxID_DLGEVENTBTNDEL, 
 wxID_DLGEVENTBTNMODIFY, wxID_DLGEVENTLCEVENTLIST, wxID_DLGEVENTRB, 
] = [wx.NewId() for _init_ctrls in range(6)]

class DlgEvent(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGEVENT, name=u'DlgEvent',
              parent=prnt, pos=wx.Point(366, 111), size=wx.Size(896, 659),
              style=wx.DEFAULT_DIALOG_STYLE, title=u'\u4e8b\u4ef6\u63d0\u9192')
        self.SetClientSize(wx.Size(888, 625))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False, u'Arial'))
        self.Center(wx.BOTH)

        self.lcEventList = wx.ListCtrl(id=wxID_DLGEVENTLCEVENTLIST,
              name=u'lcEventList', parent=self, pos=wx.Point(24, 72),
              size=wx.Size(840, 536),
              style=wx.LC_HRULES | wx.LC_VRULES | wx.LC_REPORT | wx.LC_SINGLE_SEL)
        self.lcEventList.Bind(wx.EVT_LEFT_DCLICK, self.OnLcEventListLeftDclick)
        

        self.btnAdd = wx.Button(id=wxID_DLGEVENTBTNADD, label=u'\u65b0\u589e',
              name=u'btnAdd', parent=self, pos=wx.Point(24, 24),
              size=wx.Size(75, 29), style=0)
        self.btnAdd.Bind(wx.EVT_BUTTON, self.OnBtnAddButton,
              id=wxID_DLGEVENTBTNADD)

        self.btnDel = wx.Button(id=wxID_DLGEVENTBTNDEL, label=u'\u5220\u9664',
              name=u'btnDel', parent=self, pos=wx.Point(184, 24),
              size=wx.Size(75, 29), style=0)
        self.btnDel.Bind(wx.EVT_BUTTON, self.OnBtnDelButton,
              id=wxID_DLGEVENTBTNDEL)

        self.rb = wx.RadioBox(choices=[u'到期事件', u'全部事件'],
              id=wxID_DLGEVENTRB, label=u'', majorDimension=2, parent=self,
              pos=wx.Point(556, 10))
        self.rb.Show(False)
        self.rb.Bind(wx.EVT_RADIOBOX, self.OnRbRadiobox, id=wxID_DLGEVENTRB)

        self.btnModify = wx.Button(id=wxID_DLGEVENTBTNMODIFY,
              label=u'\u4fee\u6539', name=u'btnModify', parent=self,
              pos=wx.Point(104, 24), size=wx.Size(75, 29), style=0)
        self.btnModify.Bind(wx.EVT_BUTTON, self.OnBtnModifyButton,
              id=wxID_DLGEVENTBTNMODIFY)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.myInit()

    def myInit(self):
        cols = [(CDataEvent.iId, wx.LIST_FORMAT_LEFT, -1),
                (CDataEvent.iTitle, wx.LIST_FORMAT_LEFT, 200),
                (CDataEvent.ibootAlarm, wx.LIST_FORMAT_LEFT, 0),
                (CDataEvent.iStartDate, wx.LIST_FORMAT_LEFT, 170),
                (CDataEvent.iContent, wx.LIST_FORMAT_LEFT, 300)]
        
        self.list = MyListCtrlDecorator(self.lcEventList, cols, CDataEvent)
        

    def OnBtnAddButton(self, event):
        dlg = DlgEventEditor.create(self)
        if(dlg.ShowModal() == wx.ID_OK):
            self.list.refresh()

    def getSelection(self):
        item = self.list.getSeletion()
        if (item == -1):
            return None
        
        val = int(self.list.getItem(item, 0))
        return CDataEvent.getFirstInstance({CDataEvent.iId:val})

    def OnBtnDelButton(self, event):
        obj = self.getSelection()
        if (obj is None):
            dlg = wx.MessageDialog(self, u'请选择列表项目', u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        obj.delete()
        self.list.refresh()    

    def OnRbRadiobox(self, event):
        print u'EvtRadioBox: %d-%s\n' % (event.GetInt(), event.GetString())

    def showEditor(self, edit=True):
        obj = self.getSelection()
        if obj is None:
            return
        
        dlg = DlgEventEditor.create(self)
        dlg.setData(obj)
        if(dlg.ShowModal() == wx.ID_OK):
            self.list.refresh()
            
    def OnBtnModifyButton(self, event):
        self.showEditor()

    def OnLcEventListLeftDclick(self, event):
        self.showEditor()

    def OnLcEventListListItemSelected(self, event):
        event.Skip()

    def OnLcEventListSize(self, event):
        event.Skip()
